package com.cjst.service.impl.sys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.cjst.pojo.sys.SysOffice;
import com.cjst.pojo.sys.SysUser;
import com.cjst.sdp.util.ZTreeNode;
import com.cjst.service.impl.base.BaseServiceImpl;
import com.cjst.service.sys.SysOfficeService;
import com.cjst.sql.C;
import com.cjst.sql.Method;
import com.cjst.sql.WherePrams;
import com.cjst.util.UserPrincipalUtil;

/**
 * 机构相关  服务层
 * @author yj
 *
 */
@Service
public class SysOfficeServiceImpl extends BaseServiceImpl<SysOffice, String> implements SysOfficeService{
	/**
	 * 得到公司树形列表
	 */
	@Override
	public List<ZTreeNode> tree(SysUser user,String type){
		WherePrams wherePrams=Method.where("status", C.EQ, "1");
		if(!"-1".equals(type)) {
			wherePrams.and("type", C.EQ, type);
		}
		Map<String, Object> authMap = UserPrincipalUtil.getOfficePrincipals(user);
		if("-1".equals(authMap.get("auth"))) {
			return null;
		}else if("2".equals(authMap.get("auth"))){
			wherePrams.andIn("id", C.IN, (List<String>)authMap.get("list"));
		}
		List<SysOffice> officeList=this.list(wherePrams);
		List<ZTreeNode> nodeList=new ArrayList<ZTreeNode>();
		if(officeList!=null && officeList.size()>0){
			for (SysOffice office : officeList) {
				ZTreeNode node=new ZTreeNode();
				node.setId(office.getId());
				node.setpId(office.getParentId());
				node.setName(office.getName());
				nodeList.add(node);
			}
		}
		return nodeList;
	}
}
